<?php
/**
 * User: Allen
 * Date: 15-10-19
 * 抽奖列表控制器
 */

class User_code extends PC_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->load->model('user_code_model');
	}
	
	/**
	 * @brief 抽奖信息页面
	 * @param Null
	 * @par 2015/10/19 Ver 1.00 Created by Allen
	 */	
	public function code_list() {
		$this->data['create_time'] =  empty($_POST) ? '' : $_POST['create_time'];
		$this->data['code_id'] =  empty($_POST) ? '' : $_POST['code_id'];
		$this->data['exchange_status'] =  empty($_POST) ? '2' : $_POST['exchange_status'];
		$this->load->view('/www/code_list.php',$this->data);
	}
	
	/**
	 * @brief ajxa获取保险公司信息
	 * @param Null
	 * @par 2015/10/19 Ver 1.00 Created by Allen
	 */
	public function ajax_code_list() {
		$data = $_GET;
		$start = $data['iDisplayStart']; //显示的起始索引
		$length = $data['iDisplayLength'];//显示的行数
		$sort_th = $data['mDataProp_'.$data['iSortCol_0'].''];//被排序的列 
		$sort_type = $data['sSortDir_0'];//排序的方向 "desc" 或者 "asc".
		$search = $data ['sSearch'];//全局搜索字段
		$data['code_id'] ? $where['user_code.code_id like'] = '%'.trim($data['code_id']).'%' :'';
		if($data['exchange_status'] == 1) {
			$where['user_code.exchange_status'] = 1;
		}else if($data['exchange_status'] == 0) {
			$where['user_code.exchange_status'] = 0;
		}
		$data['create_time'] ? $where['user_code.create_time >='] = trim(strtotime($data['create_time'])) :'';
		$data['create_time'] ? $where['user_code.create_time <'] = trim(strtotime($data['create_time']) + 60*60*24) :'';
		$where['concat(user_code.id) like'] = '%'.trim($search).'%';
		$this->data['count'] = $this->user_code_model->getCount($where);
		$aaData = array();
		if($this->data['count']) {
			$order_by = $sort_th." ".$sort_type;
			$code_list = $this->user_code_model->getUsercodeList($where,$length,$start,$order_by);
			//echo $this->db->last_query();exit;
			$this->load->helper('string_helper');
			foreach($code_list as $key => $value) {
				if($value['create_time']) {
					$code_list[$key]['create_time'] = date('Y-m-d H:i',$code_list[$key]['create_time']);
				}else {
					$code_list[$key]['create_time'] = '';
				}
				if($value['exchange_status']) {
					$code_list[$key]['exchange_status'] = '已兑换';
				}else {
					$code_list[$key]['exchange_status'] = '未兑换';
				}
				if($value['win_status']) {
					$code_list[$key]['win_status'] = '已中奖';
				}else {
					$code_list[$key]['win_status'] = '未中奖';
				}
				if($value['win_status'] == 1) {
					$del_oper = "onclick=\"if(confirm('确实要将此记录设为未中奖状态吗？')) return true;else return false; \"";
					$status = 0;
					$del_text = '未中奖';
				}else {
					$del_oper = "onclick=\"if(confirm('确实要将此记录设为已中奖状态吗？')) return true;else return false; \"";
					$status = 1;
					$del_text = '已中奖';
						
				}
				if($value['exchange_status'] == 1) {
					$exchange_oper = "onclick=\"if(confirm('确实要将此记录设为未兑换状态吗？')) return true;else return false; \"";
					$exchange_status = 0;
					$exchange_text = '未兑换';
				}else {
					$exchange_oper = "onclick=\"if(confirm('确实要将此记录设为已兑换状态吗？')) return true;else return false; \"";
					$exchange_status = 1;
					$exchange_text = '已兑换';
						
				}				
				if($value['remark']) {
					$value['intro'] = cn_substr_utf8($value['remark'],70);
					if($value['intro'] == $value['remark']) {
						$value['intro'] = $value['remark'];
					}else {
						$value['intro'] = $value['intro']."...";
					}
				}else {
					$value['intro'] = '';
				}
				$code_list[$key]['remark'] = '<div class="intro" style="position:relative;cursor:pointer;">'.$value['intro'].'<div class="ser_intro">'.$value['remark'].'<i></i></div></div>';
				$win_url = '<a href="/www/user_code/win?id='.$value['id'].'&win_status='.$status.'"  '.$del_oper.'><span class="btn btn-xs btn-danger cancel">'.$del_text.'</span></a>';				
				$exchange_url = '<a href="/www/user_code/exchange?id='.$value['id'].'&exchange_status='.$exchange_status.'"  '.$exchange_oper.'><span class="btn btn-xs btn-success cancel">'.$exchange_text.'</span></a>';
				$edit_url = '<a href="/www/user_code/edit_page/'.$value['id'].'"><span class="btn btn-xs btn-primary">编辑</span></a>';
				$code_list[$key]['operate'] = $edit_url." ".$win_url." ".$exchange_url;
			}
			$aaData = $code_list;
	
		}
		$output['aaData'] = $aaData;
		$output['sEcho'] = $_GET['sEcho'];
		$output['iTotalDisplayRecords'] =  $this->data['count']; //总共有几条数据
		$output['iTotalRecords'] = $this->data['count']; //总共有几条数据
		echo json_encode($output); //最后把数据以json格式返回
	}
	
	/**
	 * @brief 进入编辑页面
	 * @param Null
	 * @par 2015/10/19 Ver 1.00 Created by Allen
	 */
	public function edit_page() {
		$id = $this->uri->segment(4);
		$where['id'] = $id;
		$code_info = $this->user_code_model->checkUsercode($where);
		$this->data['data'] = $code_info;
		$this->load->view('/www/code_edit',$this->data);
	}
			
	/**
	 * @brief 保存编辑信息
	 * @param Null
	 * @par 2015/10/19 Ver 1.00 Created by Allen
	 */
	public function edit() {
		$data = $_POST;
		$data['update_time'] = time();
		$where['id'] = $data['id'];
		$edit_result = $this->user_code_model->editUsercode($where,$data);
		$go_url = '/www/user_code/code_list';
		if($edit_result) {
			$this->location_href($this->lang->line('edit_success'),$go_url);	
		}else {
			$this->location_href($this->lang->line('edit_false'),$go_url);	
		}
	}
	
	/**
	 * @brief 操作抽奖兑换状态
	 * @param Null
	 * @par 2015/10/19 Ver 1.00 Created by Allen
	 */
	public function exchange() {
		$data = $_GET;
		$id = $data['id'];
		$data['update_time'] = time();
		$where['id'] = $id;
		$exchange_result = $this->user_code_model->editUsercode($where,$data);
		$go_url = '/www/user_code/code_list';
		if($exchange_result) {
			$this->location_href($this->lang->line('operate_success'),$go_url);
		}else {
			$this->location_href($this->lang->line('operate_false'),$go_url);
		}
	}
	
	/**
	 * @brief 操作会员中奖状态
	 * @param Null
	 * @par 2015/10/19 Ver 1.00 Created by Allen
	 */
	public function win() {
		$data = $_GET;
		$id = $data['id'];
		$data['update_time'] = time();
		$where['id'] = $id;
		$exchange_result = $this->user_code_model->editUsercode($where,$data);
		$go_url = '/www/user_code/code_list';
		if($exchange_result) {
			$this->location_href($this->lang->line('operate_success'),$go_url);
		}else {
			$this->location_href($this->lang->line('operate_false'),$go_url);
		}
	}
}
?>